/* ************************************************************************

   Copyright: Prince Samuel

   License: MPL

   Authors: Prince Samuel

************************************************************************ */

/*
 * Class to represent the main window of the app.
 */

/* ************************************************************************

#asset(qx/icon/${qx.icontheme}/22/actions/view-refresh.png)

************************************************************************ */

qx.Class.define("ohmweb.MainWindow",
{
  extend : qx.ui.container.Composite,

  construct : function()
  {
    this.base(arguments)

    var layout = new qx.ui.layout.VBox;
    layout.setSeparator("separator-vertical");

    this.setLayout(layout);

    var toolbar = new qx.ui.toolbar.ToolBar();
    this.add(toolbar);


    this.__tree = new qx.ui.tree.VirtualTree(null, "Text", "Children");
    this.__tree.setIconPath("ImageURL");
    this.__nodeInfo = {};

    this.__tree.addListener("close", function(e){
        this.__nodeInfo[e.getData().getId()] = false;
    }, this);

    this.__tree.addListener("open", function(e){
        this.__nodeInfo[e.getData().getId()] = true;
    }, this);

    this.add(this.__tree, {flex: 1});

    var part1 = new qx.ui.toolbar.Part();   

    var reloadButton = new qx.ui.toolbar.Button("Refresh Tree", 
            "icon/22/actions/view-refresh.png");
    part1.add(reloadButton);

    //fire the reload event on button press
    reloadButton.addListener("execute", function() {
      this.fireEvent("reload");
    }, this);

    toolbar.add(part1);

    var part2 = new qx.ui.toolbar.Part();   

    var autoCheckBox = new qx.ui.form.CheckBox("Auto Refresh");
    part2.add(autoCheckBox);

    var slider = new qx.ui.form.Slider().set({
        minimum: 1,
        maximum: 10,
        singleStep: 1,
        pageStep: 2,
        value: 3,
        width: 100,
        height: 15
    });

    var box = new qx.ui.container.Composite(new qx.ui.layout.Basic());
    box.add(new qx.ui.basic.Label("Refresh Rate (sec)"), {top: 0, left: 10});
    box.add(slider, {top: 20, left: 10});

    var sliderLabel = new qx.ui.basic.Label("");
    box.add(sliderLabel, {top: 20, left: 120});
    
    // bind the slider value to the label content
    slider.bind("value", sliderLabel, "value");

    part2.add(box);
    toolbar.add(part2);

    var timer = new qx.event.Timer(slider.getValue()*1000);
    slider.bind("value", timer, "interval", {
        converter : function(data) {
            return data*1000; //convert to milliseconds. timer expects ms.
        }
    });

    //bind checkbox to timer
    autoCheckBox.bind("value", timer, "enabled");

    //fire the timer event 
    timer.addListener("interval", function() {
      this.fireEvent("interval");
    }, this);
  },

  events : 
  {
    "reload" : "qx.event.type.Event",
    "interval" : "qx.event.type.Event"
  },

  members: {
    __tree : null,
    __nodeInfo: null,
    
    getTree : function() {
        return this.__tree;
    },

    getNodeInfo : function() {
        return this.__nodeInfo;
    },

    //not used currently
    expandTree : function() {
        this.__tree.getItems(false, true).forEach( function(o) { o.setOpen(true); } );
    },

    //not used currently
    collapseTree : function() {
        this.__tree.getItems(false, true)[0].getItems()[0].getItems().forEach( function(o) { o.setOpen(false); } );
    }
  }
});

